﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;
using DevExpress.XtraLayout.Utils;
using Framework.Domain;
using DevExpress.XtraEditors;

namespace CIIS
{
    public partial class ProtectAndCoordinate : BaseForm
    {
        t_tq_bhxt bhxt
        {
            get { return BindEntity as t_tq_bhxt; }
        }

        public ProtectAndCoordinate()
        {
            InitializeComponent();

            normalSignatureBhxtyq.InitControl("保护协调要求：", "使用民警：", "日期：", dao.SymjDcp);
            normalSignatureCbdw.InitControl("呈报单位意见：", "签名：", "日期：", dao.OfficeDcp);
            normalSignatureTgbm.InitControl("特管部门意见：", "签名：", "日期：", dao.OfficeDcp);
            normalSignatureFjzd.InitControl("分局(支队)意见：", "签名：", "日期：", dao.OfficeDcp);
            normalSignatureSjld.InitControl("领导审批：", "签名：", "日期：", dao.UserDcp);
            normalSignatureXtjg.InitControl("协调结果：", "签名：", "日期：", dao.UserDcp);

            DevControlUtils.Instance.InitLookUpEdit(lookUpEditTqxm, dao.TqryDcp);
            lookUpEditTqxm.EditValueChanged += new EventHandler(lookUpEditTqxm_EditValueChanged);

            buttonSave.Click += new EventHandler(buttonSave_Click);
            buttonSave.DialogResult = DialogResult.OK;
            buttonBack.Click += new EventHandler(buttonBack_Click);
        }

        void lookUpEditTqxm_EditValueChanged(object sender, EventArgs e)
        {
            textEditTqdh.Text = lookUpEditTqxm.EditValue.ToString();
        }

        private void InitControls()
        {
            if (OptType == EOperateType.Detail)
            {
                layoutControlItemSave.Visibility = LayoutVisibility.Never;
            }
        }

        void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (OptType == EOperateType.Add)
                {
                    BindEntity.State.MarkNew();
                }
                else
                {
                    BindEntity.State.MarkDirty();
                }

                bhxt.BHXTYQ = normalSignatureBhxtyq.Yj; 
                bhxt.BHXTYQ_SYMJ = normalSignatureBhxtyq.Qz; 
                bhxt.BHXTYQ_SYSJ = normalSignatureBhxtyq.Qzsj;

                bhxt.CBDW_YJ = normalSignatureCbdw.Yj; 
                bhxt.CBDW_QZ = normalSignatureCbdw.Qz; 
                bhxt.CBDW_CBSJ = normalSignatureCbdw.Qzsj;

                bhxt.FJZD_YJ = normalSignatureFjzd.Yj; 
                bhxt.FJZD_QZ = normalSignatureFjzd.Qz; 
                bhxt.FJZD_SHSJ = normalSignatureFjzd.Qzsj;

                bhxt.SJLD_YJ = normalSignatureSjld.Yj;
                bhxt.SJLD_QZ = normalSignatureSjld.Qz; 
                bhxt.SJLD_SPSJ = normalSignatureSjld.Qzsj;

                bhxt.TGBM_YJ = normalSignatureTgbm.Yj;
                bhxt.TGBM_QZ = normalSignatureTgbm.Qz; 
                bhxt.TGBM_SHSJ = normalSignatureTgbm.Qzsj;

                bhxt.XTJG_YJ = normalSignatureXtjg.Yj; 
                bhxt.XTJG_QZ = normalSignatureXtjg.Qz; 
                bhxt.XTJG_XTSJ = normalSignatureXtjg.Qzsj;

                t_tq_bhxt o = dao.Dao.SaveOrUpdate(BindEntity) as t_tq_bhxt;

                XtraMessageBox.Show("保存成功");
            }
            catch
            {
                XtraMessageBox.Show("保存失败");
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// 绑定数据源，在show窗口之前需要调用
        /// </summary>
        /// <param name="tqry"></param>
        public override void BindData(Entity entity)
        {
            InitControls();

            BindEntity = entity as t_tq_bhxt;
            if (BindEntity == null)
            {
                BindEntity = new t_tq_bhxt();
                bhxt.State.MarkNew();
                bhxt.Id = Guid.NewGuid().ToString().Replace("-", string.Empty);
            }

            if (bhxt != null)
            {
                textEditTbrxm.DataBindings.Add("Text", bhxt, "CZY");
                textEditTqdh.DataBindings.Add("Text", bhxt, "RYDH");

                memoEditSajyaq.DataBindings.Add("Text", bhxt, "SAJYAQ");
                memoEditZygx.DataBindings.Add("Text", bhxt, "ZYGX");

                lookUpEditBxtdw.DataBindings.Add("EditValue", bhxt, "BXTDW");
                lookUpEditDjdw.DataBindings.Add("EditValue", bhxt, "DJDW");
                lookUpEditTqxm.DataBindings.Add("EditValue", bhxt, "RYDH");

                normalSignatureBhxtyq.SetValues(new object[] { bhxt.BHXTYQ, bhxt.BHXTYQ_SYMJ, bhxt.BHXTYQ_SYSJ });
                normalSignatureCbdw.SetValues(new object[] { bhxt.CBDW_YJ, bhxt.CBDW_QZ, bhxt.CBDW_CBSJ });
                normalSignatureFjzd.SetValues(new object[] { bhxt.FJZD_YJ, bhxt.FJZD_QZ, bhxt.FJZD_SHSJ });
                normalSignatureSjld.SetValues(new object[] { bhxt.SJLD_YJ, bhxt.SJLD_QZ, bhxt.SJLD_SPSJ });
                normalSignatureTgbm.SetValues(new object[] { bhxt.TGBM_YJ, bhxt.TGBM_QZ, bhxt.TGBM_SHSJ });
                normalSignatureXtjg.SetValues(new object[] { bhxt.XTJG_YJ, bhxt.XTJG_QZ, bhxt.XTJG_XTSJ });

            }
        }
    }
}
